package com.cn.sys.main.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.sys.frame.util.page.PageData;
import com.cn.sys.frame.util.query.QueryBean;
import com.cn.sys.frame.util.query.QueryCondition;
import com.cn.sys.frame.web.BaseController;
import com.cn.sys.main.entity.SysDictionary;
import com.cn.sys.main.service.SysDictionaryService;

@Controller
@RequestMapping("sys/dict")
public class SysDictController extends BaseController {

	@Autowired
	private SysDictionaryService sysDictionaryService;
	
	@RequestMapping(value = "/toList.do", method = RequestMethod.GET)
	public String toList(HttpServletRequest request, Model model){
		return "sys/dict/dictList";
	}
	
	@RequestMapping(value = "/list.do", method = RequestMethod.POST)
	@ResponseBody
	public Object list(HttpServletRequest request, String name, String parentId) throws Exception{
		PageData<SysDictionary> pageData = createPageDataFromRequest(request);
		QueryCondition queryCondition = null;
		if(StringUtils.isNotBlank(name)){
			QueryBean qb = new QueryBean(" AND NAME LIKE ?", "NAME", "%"+name+"%");
			queryCondition = new QueryCondition();
			queryCondition.setQueryBean(qb);
		}
		
		sysDictionaryService.getPageData(pageData, queryCondition);
		return pageData.getPageDataMap();
	}
	
	@RequestMapping(value = "/toAdd.do", method = RequestMethod.GET)
	public String toAdd(String id, Model model) throws Exception{
		if(StringUtils.isNotBlank(id)&&StringUtils.isNumeric(id)){
			SysDictionary dict = sysDictionaryService.get(Long.valueOf(id));
			model.addAttribute("dict", dict);
		}
		return "sys/dict/dictAdd";
	}
	
	@RequestMapping(value = "/getDictTree.do", method = RequestMethod.POST)
	@ResponseBody
	public Object getOrganTree() throws Exception{
		
		return sysDictionaryService.getAll();
	}
	
	@RequestMapping(value = "/validateDictCode.do", method = RequestMethod.POST)
	@ResponseBody
	public Object validateRoleName(String id, String code) throws Exception{
		QueryCondition queryCondition = new QueryCondition();
		if(StringUtils.isNotBlank(id)){
			QueryBean qb = new QueryBean(" AND ID <> ?", "ID", Long.valueOf(id));
			queryCondition.setQueryBean(qb);
		}
		if(StringUtils.isNotBlank(code)){
			queryCondition.setQueryBean(new QueryBean(" AND CODE = ?", "CODE", code));
		}else {
			return false;
		}
		long count = sysDictionaryService.getCount(queryCondition);
		if(count>0){
			return false;
		}
		return true;
	}
	
	@RequestMapping(value = "/save.do", method = RequestMethod.POST)
	@ResponseBody
	public Object save(HttpServletRequest request, SysDictionary dict) throws Exception{
		if(dict!=null && dict.getId()!=null){
			dict = sysDictionaryService.update(dict);
		}else {
			dict = sysDictionaryService.save(dict);
		}
		Map<String, Object> result = new HashMap<String, Object>(1);
		if(dict==null){
			result.put("success", false);
		}else {
			result.put("success", true);
		}
		return result;
	}
	
	@RequestMapping(value = "/delete.do", method = RequestMethod.POST)
	@ResponseBody
	public Object delete(HttpServletRequest request, String ids) throws Exception{
		Map<String, Object> result = new HashMap<String, Object>(2);
		if(ids!=null){
			int count = 0;
			String[] strArr = StringUtils.split(ids, ",");
			for (int i=0,len=strArr.length; i<len; i++) {
				String id = strArr[i];
				if(StringUtils.isNotBlank(id)&&StringUtils.isNumeric(id)){
					int resultCnt = sysDictionaryService.delete(Long.valueOf(id));
					if(resultCnt == 1){
						count ++;
					}
				}
			}
			if(count == strArr.length){
				result.put("success", true);
			} else {
				result.put("success", false);
				result.put("msg", (strArr.length - count)+"条记录删除失败!");
			}
			
		}else {
			result.put("success", false);
		}
		return result;
	}
	
	@RequestMapping(value = "/toView.do", method = RequestMethod.GET)
	public String toView(String id, Model model) throws Exception{
		if(StringUtils.isNotBlank(id)&&StringUtils.isNumeric(id)){
			SysDictionary dict = sysDictionaryService.get(Long.valueOf(id));
			model.addAttribute("dict", dict);
		}
		return "sys/dict/dictView";
	}
}
